package jsyf.oa.service.forms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.forms.FormsCreateDao;
import jsyf.oa.dao.forms.FormsDao;
import jsyf.oa.dao.forms.FormsFieldsDao;
import jsyf.oa.entity.forms.ComponentsEntity;
import jsyf.oa.entity.forms.FormsCreateEntity;
import jsyf.oa.entity.forms.FormsDataEntity;
import jsyf.oa.entity.forms.FormsFieldsEntity;
import jsyf.oa.facade.forms.FormsFacade;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 92852 on 2018/6/22.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class FormsService implements FormsFacade {
    @Autowired
    private FormsDao formsDao;
    @Autowired
    private FormsFieldsDao formsFieldsDao;
    @Autowired
    private FormsCreateDao formsCreateDao;

    private ComponentsEntity componentsEntity;
    private FormsDataEntity formsDataEntity;
    private FormsFieldsEntity formsFieldsEntity;


    /*查询组件*/
    @Override
    public List<JSONObject> getComponents(String jsonData) {
        return formsDao.getComponents();
    }

    @Override
    public JSONObject saveInfo(String jsonData) {
        List<FormsFieldsEntity> entity = (List<FormsFieldsEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), FormsFieldsEntity.class);
        Integer rows = 0;
        for (int i = 0; i < entity.size(); i++) {
            FormsFieldsEntity model = entity.get(i);
            rows = formsFieldsDao.add(model);
        }
        JSONObject obj = new JSONObject();
        if (rows > 0) {
            obj.put("records", "保存成功");
        } else {
            obj.put("records", "保存失败");
        }
        return obj;
    }

    /*保存表单主要信息*/
    @Override
    public JSONObject saveCreaterInfo(String jsonData) {
        FormsCreateEntity formsCreateEntity = JSON.toJavaObject(JSON.parseObject(jsonData), FormsCreateEntity.class);
        String formsName=formsDao.getFormsName(formsCreateEntity.getFormsName());
        Integer rows = 0;
        JSONObject obj = new JSONObject();
        if (org.apache.commons.lang3.StringUtils.isBlank(formsName)){
             rows=formsCreateDao.add(formsCreateEntity);
            if (rows > 0) {
                String formsCreateId=formsDao.getFormsCreateId(formsCreateEntity.getFormsName());
                obj.put("records",formsCreateId);
            } else {
                obj.put("records", "保存失败");
            }
        }else{
            obj.put("records","已存在");
        }
        return obj;
    }

    /*查询个人表单信息*/
    @Override
    public List<JSONObject> getFormsCreate(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        List<JSONObject> records=formsDao.getFormsCreate(userId);
        return records;
    }

    @Override
    public List<JSONObject> getFieldsAndComponents(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String mainId = jsonParams.getString("createId");
        List<JSONObject> records=formsDao.getFieldsAndComponents(mainId);
        return records;

    }


}